home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP10.TXT < prev    next >
Text File  |  1987-11-21  |  18KB  |  455 lines

  1.  
  2.                        Chapter 10 - File Input/Output
  3.  
  4.  
  5.                               OUTPUT TO A FILE
  6.  
  7.              Load  and  display  the file named FORMOUT.C  for  your
  8.         first example of writing data to a file.  We begin as before
  9.         with the "include" statement for "stdio.h", then define some
  10.         variables for use in the example including a rather  strange
  11.         looking new type.
  12.  
  13.              The  type  "FILE"  is used for a file variable  and  is
  14.         defined in the "stdio.h" file.   It is used to define a file
  15.         pointer  for use in file operations.   The definition  of  C
  16.         contains  the requirement for a pointer to a "FILE",  and as
  17.         usual, the name can be any valid variable name.
  18.  
  19.                                OPENING A FILE
  20.  
  21.              Before we can write to a file,  we must open it.   What
  22.         this  really means is that we must tell the system  that  we
  23.         want  to  write to a file and what the filename is.   We  do
  24.         this with the "fopen" function illustrated in the first line
  25.         of the program.   The file pointer, "fp" in our case, points
  26.         to   the  file  and  two  arguments  are  required  in   the
  27.         parentheses,  the filename first, followed by the file type.
  28.         The filename is any valid DOS filename, and can be expressed
  29.         in  upper  or lower case letters,  or even mixed if  you  so
  30.         desire.   It is enclosed in double quotes.  For this example
  31.         we have chosen the name TENLINES.TXT.   This file should not
  32.         exist  on your disk at this time.   If you have a file  with
  33.         this  name,  you should change its name or move  it  because
  34.         when  we execute this program,  its contents will be erased.
  35.         If you don't have a file by this name,  that is good because
  36.         we will create one and put some data into it.
  37.  
  38.                                READING ("r")
  39.  
  40.              The  second parameter is the file attribute and can  be
  41.         any  of three letters, "r", "w", or "a", and must  be  lower
  42.         case. There are actually additional attributes available  in
  43.         C to allow more flexible I/O.  When an "r" is used, the file
  44.         is  opened for reading, a "w" is used to indicate a file  to
  45.         be used for writing, and an "a" indicates that you desire to
  46.         append  additional data to the data already in  an  existing
  47.         file.   Opening  a file for reading requires that  the  file
  48.         already exist.  If it does not exist, the file pointer  will
  49.         be set to NULL and can be checked by the program.
  50.  
  51.                                WRITING ("w")
  52.  
  53.              When  a file is opened for writing,  it will be created
  54.         if it does not already exist and it will be reset if it does
  55.         resulting in deletion of any data already there.
  56.  
  57.  
  58.                                    Page 70
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                        Chapter 10 - File Input/Output
  69.  
  70.  
  71.  
  72.                               APPENDING ("a")
  73.  
  74.              When a file is opened for appending, it will be created
  75.         if it does not already exist and it will be initially empty.
  76.         If  it does exist,  the data input point will be the end  of
  77.         the  present data so that any new data will be added to  any
  78.         data that already exists in the file.
  79.  
  80.                            OUTPUTTING TO THE FILE
  81.  
  82.              The  job of actually outputting to the file  is  nearly
  83.         identical  to  the  outputting we have already done  to  the
  84.         standard output device.   The only real differences are  the
  85.         new  function names and the addition of the file pointer  as
  86.         one  of  the function arguments.   In the  example  program,
  87.         "fprintf" replaces our familiar "printf" function name,  and
  88.         the  file  pointer  defined earlier is  the  first  argument
  89.         within  the  parentheses.   The remainder of  the  statement
  90.         looks  like,  and  in  fact is identical  to,  the  "printf"
  91.         statement.
  92.  
  93.                                CLOSING A FILE
  94.  
  95.              To close a file,  you simply use the function  "fclose"
  96.         with the file pointer in the parentheses.  Actually, in this
  97.         simple  program,  it  is  not necessary to  close  the  file
  98.         because   the  system  will  close  all  open  files  before
  99.         returning to DOS.  It would be good programming practice for
  100.         you to get in the habit of closing all files in spite of the
  101.         fact  that they will be closed automatically,  because  that
  102.         would act as a reminder to you of what files are open at the
  103.         end of each program.
  104.  
  105.              You can open a file for writing,  close it,  and reopen
  106.         it  for  reading,  then  close it,  and open  it  again  for
  107.         appending,  etc.   Each time you open it,  you could use the
  108.         same file pointer,  or you could use a different  one.   The
  109.         file  pointer  is simply a tool that you use to point  to  a
  110.         file and you decide what file it will point to.
  111.  
  112.              Compile  and run this program.   When you run  it,  you
  113.         will  not  get any output to the monitor because it  doesn't
  114.         generate any.   After running it, look at your directory for
  115.         a file named TENLINES.TXT and "type" it.  That is where your
  116.         output will be.   Compare the output with that specified  in
  117.         the program.  It should agree.
  118.  
  119.              Do not erase the file named TENLINES.TXT yet.   We will
  120.         use it in some of the other examples in this chapter.
  121.  
  122.  
  123.  
  124.                                    Page 71
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                        Chapter 10 - File Input/Output
  135.  
  136.  
  137.  
  138.                   OUTPUTTING A SINGLE CHARACTER AT A TIME
  139.  
  140.              Load the next example file,  CHAROUT.C,  and display it
  141.         on your monitor.  This program will illustrate how to output
  142.         a single character at a time.
  143.  
  144.              The  program begins with the "include" statement,  then
  145.         defines  some variables including a file pointer.   We  have
  146.         called the file pointer "point" this time, but we could have
  147.         used any other valid variable name.  We then define a string
  148.         of characters to use in the output function using a "strcpy"
  149.         function.   We are ready to open the file for appending  and
  150.         we  do so in the "fopen" function,  except this time we  use
  151.         the  lower cases for the filename.   This is done simply  to
  152.         illustrate  that  DOS  doesn't care about the  case  of  the
  153.         filename.  Notice that the file will be opened for appending
  154.         so  we  will  add  to the lines  inserted  during  the  last
  155.         program.
  156.  
  157.              The  program is actually two nested "for"  loops.   The
  158.         outer  loop  is  simply a count to ten so that  we  will  go
  159.         through the inner loop ten times.   The inner loop calls the
  160.         function  "putc" repeatedly until a character in "others" is
  161.         detected to be a zero.
  162.  
  163.                             THE "putc" FUNCTION
  164.  
  165.              The  part  of the program we are interested in  is  the
  166.         "putc" function.   It outputs one character at a  time,  the
  167.         character  being  the first argument in the parentheses  and
  168.         the  file pointer being the second and last  argument.   Why
  169.         the  designer of C made the pointer first in  the  "fprintf"
  170.         function, and last in the "putc" function is a good question
  171.         for which there may be no answer.   It seems like this would
  172.         have been a good place to have used some consistency.
  173.  
  174.              When  the textline "others" is exhausted,  a newline is
  175.         needed because a newline was not included in the  definition
  176.         above.   A  single "putc" is then executed which outputs the
  177.         "\n" character to return the carriage and do a linefeed.
  178.  
  179.              When  the outer loop has been executed ten  times,  the
  180.         program  closes the file and terminates.   Compile  and  run
  181.